﻿using BLL;
using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using webui.Models;

namespace webui.Areas.Admin.Controllers
{
    public class ShopController : Controller
    {
        ShopBLL bll = new ShopBLL();
        // GET: Admin/CarBrand
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [Skip]
        public ActionResult DataList(PageRequst req)
        {
            int pageIndex = req.page == 0 ? 1 : req.page;
            int pageSize = req.rows == 0 ? 1 : req.rows;
            PageModel page = new PageModel()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                PageCount = 0
            };
            List<IConditionalModel> liswhere = new List<IConditionalModel>();
            if (!string.IsNullOrEmpty(req.KeyWord))
            {
                liswhere.Add(new ConditionalModel() { FieldName = "ShopName", ConditionalType = ConditionalType.Like, FieldValue = req.KeyWord });
            }
            List<Shop> list = bll.GetPageList(liswhere, page, u => u.OrderNum);
            return Json(new DataGridModel() { rows = list, total = page.PageCount });
        }

        /// <summary>
        /// 返回添加用户页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [SkipPemission]
        public ActionResult AddPage()
        {
            return PartialView("EditPage");
        }

        [HttpGet]
        [SkipPemission]
        public ActionResult EditPage(Shop brand)
        {
            Shop card = new Shop();

            if (brand.ID != 0)
            {
                card = bll.GetSingle(u => u.ID == brand.ID);
            }
            return PartialView(card);
        }

        [HttpPost]
        public ActionResult EditSave(Shop brand)
        {
            if (brand.ID != 0)
            {
                Shop dbshop = bll.GetSingle(u => u.ID == brand.ID);
                dbshop.ShopName = brand.ShopName;
                dbshop.Bz = brand.Bz;
                dbshop.OrderNum = brand.OrderNum;
                bll.Update(dbshop);
            }
            else
            {
                brand.Createtime = DateTime.Now;
                brand.CreateUserID = OperateContext.Current.Usr.username;

                bll.Insert(brand);
            }
            return Redirect("/admin/Shop/index");
            // return OperateContext.Current.RedirectAjax("ok", "保存成功", null, "");
        }

        /// <summary>
        /// 删除账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(int id)
        {
            Car_Brand card = new Car_Brand();

            if (id != 0)
            {
                bll.Delete(u => u.ID == id);
            }
            return OperateContext.Current.RedirectAjax("ok", "删除成功", null, "");
        }
    }
}